package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.druid.wall.violation.ErrorCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingPlanProject;
import com.zzyl.entity.NursingProject;
import com.zzyl.exception.BaseException;
import com.zzyl.exception.GlobalExceptionHandler;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {


    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页条件查询护理项目
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse selectByPage(String name, Integer status, Integer pageNum, Integer pageSize) {

        //设置分页
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> nursingProjects = nursingProjectMapper.selectByPage(name, status);
        return PageResponse.of(nursingProjects, NursingProjectVo.class);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDTO 护理项目数据传输对象
     */
    @Override
    public void add(NursingProjectDto nursingProjectDTO) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDTO, nursingProject);
        nursingProject.setCreateBy(UserThreadLocal.getUserId() == null ? 1 : UserThreadLocal.getUserId());
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据ID查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.findById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    /**
     * 更新护理项目信息
     *
     * @param nursingProjectDTO 护理项目数据传输对象
     */
    @Override
    public void update(NursingProjectDto nursingProjectDTO) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDTO, nursingProject);
        nursingProject.setUpdateBy(UserThreadLocal.getUserId() == null ? 1 : UserThreadLocal.getUserId());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 获取全部护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> getAllProject() {
        return nursingProjectMapper.getAllProject();
    }

    @Override
    public ResponseResult deleteById(Long id) {
        //查询护理项目是否被使用
        NursingPlanProject planProject = nursingProjectMapper.isPlanProject(id);
        if (planProject != null) {
            nursingProjectMapper.deleteById(id);
            return ResponseResult.success();
        } else {
            return ResponseResult.error("该护理项目已被使用，无法删除!");
        }

    }

    @Override
    public void updateStatusById(Long id, Integer status) {
        nursingProjectMapper.updateStatusById(id, status);
    }


}
